//
//  YSHUD.m
//  YSSmartHome
//
//  Created by yanghao on 2017/4/11.
//  Copyright © 2017年 yahaong. All rights reserved.
//

#import "YSHUD.h"
#import "GatewayPermitView.h"
#import "YSCommon.h"

@interface YSHUD ()
<
GatewayPermitViewDelegate,
AlarmViewDelegate
>
@property (nonatomic, strong) UIView *overlayView;
@end
@implementation YSHUD

static YSHUD* _instance;
+ (YSHUD*)instance
{
    if (!_instance) {
        _instance = [[YSHUD alloc]init];
    }
    return _instance;
}
+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
    if (!_instance)
    {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            _instance = [super allocWithZone:zone];
        });
    }
    return _instance;
}

- (id)copyWithZone:(NSZone *)zone
{
    return _instance;
}

-(id)mutableCopyWithZone:(NSZone *)zone {
    return _instance;
}

+ (void)showGatewayPermit:(ZTGatewayBindModel*)model
{
    [[self instance] addOverlayViewToWindow];
    GatewayPermitView* permitView = [GatewayPermitView gatewayPermitView];
    permitView.model = model;
    [[self instance].overlayView addSubview:permitView];
    [permitView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo([self instance].overlayView);
    }];
    permitView.delegate = [self instance];
}
+ (void)showAlarmView:(ZTOneControlDevice*)device
{
    [[self instance] addOverlayViewToWindow];
    AlarmView* alartView = [[AlarmView alloc]init];
    [[self instance].overlayView addSubview:alartView];
    [alartView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo([self instance].overlayView);
        make.leading.equalTo([self instance].overlayView).offset(kIphoneSize_Widith(30));
        make.trailing.equalTo([self instance].overlayView).offset(kIphoneSize_Widith(-30));
    }];
    alartView.device = device;
    alartView.delegate = [self instance];
}
- (void)alarmViewDidTappedHandle:(AlarmView* __nonnull)alarmView
{
    [alarmView removeFromSuperview];
    if (self.overlayView.subviews.count == 0)
    {
        [self.overlayView removeFromSuperview];
    }

}
- (void)gatewayPermitWatch:(GatewayPermitView* __nonnull)gatewayPermitView type:(GatewayPermitType)type
{
    [gatewayPermitView removeFromSuperview];
    if (self.overlayView.subviews.count == 0) {
        [self.overlayView removeFromSuperview];
    }
}

- (void)addOverlayViewToWindow
{
    if (!self.overlayView.superview)
    {
        NSEnumerator *frontToBackWindows = [UIApplication.sharedApplication.windows reverseObjectEnumerator];
        for (UIWindow *window in frontToBackWindows)
        {
            BOOL isWindow = [window isKindOfClass:[UIWindow class]];
            BOOL windowOnMainScreen = CGRectEqualToRect(window.bounds, [UIScreen mainScreen].bounds);
            BOOL windowIsVisible = !window.hidden && window.alpha > 0;
            if (isWindow && windowOnMainScreen && windowIsVisible) {
                [window addSubview:self.overlayView];
                [self.overlayView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.edges.equalTo(window);
                }];
                break;
            }
        }
    }
    else
    {
        [self.overlayView.superview bringSubviewToFront:self.overlayView];
    }
}

#pragma mark - getters and setters 
- (UIView *)overlayView {
    if(!_overlayView) {
        CGRect windowBounds = [UIApplication sharedApplication].keyWindow.bounds;
        _overlayView = [[UIView alloc] initWithFrame:windowBounds];
        _overlayView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    }
    return _overlayView;
}


@end
